﻿using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace Tender.Tool.Server.Database
{
    public class SqlserverData : IDatabase
    {
        private readonly IConfiguration configurationSection;
        public SqlserverData(IConfiguration configurationSection)
        {
            this.configurationSection = configurationSection;
        }
        /// <summary>
        /// 
        /// </summary>
        private SqlConnection? connection_;
        private string? SqlConfigstr_;
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string SqlConfigstr
        {
            get
            {
                if (string.IsNullOrEmpty(SqlConfigstr_))
                {
                    //string file = System.Windows.Forms.Application.ExecutablePath;
                    //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);
                    SqlConfigstr_ = configurationSection.GetSection("DataBase").Get<DataBaseConfig>()?.SQL; ;
                    return SqlConfigstr_;
                }
                else
                {

                    return SqlConfigstr_;
                }
            }
            set
            {
                if (connection_ != null && SqlConfigstr != connection_.ConnectionString)
                {
                    connection_ = new SqlConnection(SqlConfigstr);
                }
                SqlConfigstr_ = value;
            }
        }

        public SqlConnection connection
        {
            get
            {
                if (connection_ != null && SqlConfigstr != connection_.ConnectionString)
                {
                    connection_ = new SqlConnection(SqlConfigstr);
                    return connection_;
                }
                if (connection_ == null)
                {
                    connection_ = new SqlConnection(SqlConfigstr);
                    return connection_;
                }
                else
                {
                    return connection_;
                }


            }
        }
        private DataBaseSelect dataBaseSelect_;
        /// <summary>
        /// 选择查询那个数据库
        /// </summary>
        public DataBaseSelect dataBaseSelect
        {
            get
            {
                return dataBaseSelect_;
            }
            set
            {
                switch (value)
                {
                    case DataBaseSelect.Mysql:
                        SqlConfigstr = configurationSection.GetSection("DataBase").Get<DataBaseConfig>()?.Mysql;
                        break;
                    case DataBaseSelect.SQL:
                        SqlConfigstr = configurationSection.GetSection("DataBase").Get<DataBaseConfig>()?.SQL;
                        break;
                    case DataBaseSelect.FlowEngin:
                        SqlConfigstr = configurationSection.GetSection("DataBase").Get<DataBaseConfig>()?.FlowEngin;
                        break;
                    default:
                        break;
                }
                dataBaseSelect_ = value;
            }
        }

        public Task<DataTable> Where(string sql)
        {
            throw new NotImplementedException();
        }

        public DataTable Where(string sql, out string excption)
        {
            excption = "";
            try
            {
                using (SqlDataAdapter cmd = new SqlDataAdapter(sql, connection))
                {
                    DataTable dt = new DataTable();
                    cmd.Fill(dt);
                    connection.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                excption = ex.Message;
            }
            return null;
        }

        public List<T> Where<T>(string sql, out string exception) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public Task<List<T>> Where<T>(string sql) where T : class, new()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// DataTable转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        private List<T> ListData<T>(DataTable dataTable) where T : class, new()
        {
            var list = new List<T>();
            var ps = typeof(T).GetProperties();

            //遍历所有DataTable的行
            foreach (DataRow dr in dataTable.Rows)
            {
                var t = new T();
                //通过反射获取T类型的所有成员
                foreach (PropertyInfo pi in ps)
                {
                    if (dataTable.Columns.Contains(pi.Name) && dr[pi.Name] != DBNull.Value)
                    {

                        object value = Convert.ChangeType(dr[pi.Name], pi.PropertyType, null);
                        pi.SetValue(t, value, null);
                    }
                }
                //将T类型添加到集合list
                list.Add(t);
            }
            return list;
        }
    }
}
